<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Controllers\AdminController as Controller;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;

class CategoryController extends Controller
{
    use ModelForm;

    protected $title = "商品分类管理";

    public function index(\Encore\Admin\Layout\Content $content)
    {
        $content->title($this->title());
        $content->row(function (Row $row) {
            $row->column(6, $this->treeView()->render());

            $row->column(6, function (Column $column) {
                $column->append($this->form(true));
            });
        });

        return $content;
    }

    protected function treeView()
    {
        return Category::tree(function (Tree $tree) {
            $tree->disableCreate();
            return $tree;
        });
    }

    protected function form($create = false)
    {
        $form = new Form(new Category());

        if ($create) {
            $form->setAction(route('admin.category.store'));
        }

        $form->display('id', 'ID');

        $form->select('parent_id', '上级分类')->options(Category::selectOptions());
        $form->text('name', '分类名称');
        $form->textarea('summary', '分类描述');
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        $form->saved(function () {
            return redirect(route('admin.category.index'));
        });

        return $form;
    }
}
